<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="漂流瓶ID" prop="bottleId">
        <el-input
          v-model="queryParams.bottleId"
          placeholder="请输入漂流瓶ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送者ID" prop="senderId">
        <el-input
          v-model="queryParams.senderId"
          placeholder="请输入发送者ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接收者ID" prop="receiverId">
        <el-input
          v-model="queryParams.receiverId"
          placeholder="请输入接收者ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="staticq">
        <el-select v-model="queryParams.staticq" placeholder="请选择状态" clearable>
          <el-option label="正常" value="1" />
          <el-option label="已屏蔽" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-lock"
          size="mini"
          :disabled="multiple"
          @click="handleBatchBlock"
        >批量屏蔽</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="multiple"
          @click="handleBatchApprove"
        >批量解除屏蔽</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:reply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="replyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="回复ID" align="center" prop="id" width="80" />
      <el-table-column label="漂流瓶ID" align="center" prop="bottleId" width="100" />
      <el-table-column label="发送者ID" align="center" prop="senderId" width="100" />
      <el-table-column label="接收者ID" align="center" prop="receiverId" width="100" />
      <el-table-column label="回复内容" align="center" prop="content" width="300" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="scope.row.content === '内容已屏蔽'" style="color: #F56C6C;">
            <i class="el-icon-warning"></i> {{ scope.row.content }}
          </span>
          <span v-else>{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="staticq" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.staticq == 1" type="success">正常</el-tag>
          <el-tag v-else-if="scope.row.staticq == 0" type="danger">已屏蔽</el-tag>
          <span v-else>{{ scope.row.staticq }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.staticq == 1"
            size="mini"
            type="text"
            icon="el-icon-remove-outline"
            @click="handleBlockSingle(scope.row)"
            style="color: #F56C6C;"
          >屏蔽</el-button>
          <el-button
            v-else-if="scope.row.staticq == 0"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleApproveSingle(scope.row)"
            style="color: #67C23A;"
          >通过审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改漂流瓶回复状态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="漂流瓶ID" prop="bottleId">
          <el-input v-model="form.bottleId" placeholder="漂流瓶ID" disabled />
        </el-form-item>
        <el-form-item label="发送者ID" prop="senderId">
          <el-input v-model="form.senderId" placeholder="发送者ID" disabled />
        </el-form-item>
        <el-form-item label="接收者ID" prop="receiverId">
          <el-input v-model="form.receiverId" placeholder="接收者ID" disabled />
        </el-form-item>
        <el-form-item label="回复内容" prop="content">
          <el-input 
            type="textarea" 
            v-model="form.content" 
            :rows="4"
            placeholder="回复内容" 
            disabled />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">已屏蔽</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReply, getReply, delReply, addReply, updateReply } from "@/api/system/reply"

export default {
  name: "Reply",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 漂流瓶回复表格数据
      replyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bottleId: null,
        senderId: null,
        receiverId: null,
        content: null,
        staticq: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bottleId: [
          { required: true, message: "对应的漂流瓶ID不能为空", trigger: "blur" }
        ],
        senderId: [
          { required: true, message: "发送者ID不能为空", trigger: "blur" }
        ],
        receiverId: [
          { required: true, message: "接收者ID不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "回复内容不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询漂流瓶回复列表 */
    getList() {
      this.loading = true
      listReply(this.queryParams).then(response => {
        this.replyList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        bottleId: null,
        senderId: null,
        receiverId: null,
        content: null,
        staticq: null,
        createTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加漂流瓶回复"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getReply(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改回复状态"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateReply(this.form).then(response => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
          })
        }
      })
    },
    /** 单个屏蔽按钮操作 */
    handleBlockSingle(row) {
      this.$modal.confirm('是否确认屏蔽该回复内容？').then(() => {
        return getReply(row.id).then(response => {
          const replyData = response.data
          replyData.staticq = 0
          return updateReply(replyData)
        })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("屏蔽成功")
      }).catch(() => {})
    },
    /** 批量屏蔽按钮操作 */
    handleBatchBlock() {
      const ids = this.ids
      this.$modal.confirm('是否确认批量屏蔽选中的回复内容？').then(() => {
        const promises = ids.map(id => {
          return getReply(id).then(response => {
            const replyData = response.data
            replyData.staticq = 0
            return updateReply(replyData)
          })
        })
        return Promise.all(promises)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("批量屏蔽成功")
      }).catch(() => {})
    },
    /** 通过审核按钮操作 */
    handleApproveSingle(row) {
      this.$modal.confirm('是否确认解除屏蔽该回复内容？').then(() => {
        return getReply(row.id).then(response => {
          const replyData = response.data
          replyData.staticq = 1
          return updateReply(replyData)
        })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("解除屏蔽成功")
      }).catch(() => {})
    },
    /** 批量解除屏蔽按钮操作 */
    handleBatchApprove() {
      const ids = this.ids
      this.$modal.confirm('是否确认批量解除屏蔽选中的回复内容？').then(() => {
        const promises = ids.map(id => {
          return getReply(id).then(response => {
            const replyData = response.data
            replyData.staticq = 1
            return updateReply(replyData)
          })
        })
        return Promise.all(promises)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("批量解除屏蔽成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reply/export', {
        ...this.queryParams
      }, `reply_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
